//
//  SceneOptions.m
//  hamsBaseball
//
//  Created by Donggi Hahm on 10. 06. 12.
//  Copyright 2010 ComTalk. All rights reserved.
//

#import "SceneOptions.h"
#import "SceneBatteryView.h"
#import "global.h"
#import "SceneMenu.h"


@implementation Options

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	Options *layer = [Options node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if ( (self=[super init] )) 
	{
		m_windowSize = [[CCDirector sharedDirector] winSize];
		
		if ( m_windowSize.width == 768 )
			m_DeviceKind = DEVICE_IPAD;
		else
			m_DeviceKind = DEVICE_IPHONE;
		
		//bg image
		spBackground = [CCSprite spriteWithFile:@"background2.png"];
		if ( m_DeviceKind == DEVICE_IPHONE )
			[spBackground setPosition:ccp(CX,CY-145)];
		else 
			[spBackground setPosition:ccp(CX,CY)];
		[self addChild:spBackground];
		
		CCSprite *spWhiteboard = [CCSprite spriteWithFile:@"whiteboard.png"];
		[spWhiteboard setPosition:ccp(CX,CY+90)];
		[self addChild:spWhiteboard];
		
		CCSprite *spKindsOfPitch = [CCSprite spriteWithFile:@"kinds_of_pitch.png"];
		[spKindsOfPitch setPosition:ccp(CX,CY+90)];
		[self addChild:spKindsOfPitch];
		
		//bg sub image
		spBall1 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall1 setScale:0.7f];
		[spBall1 setPosition: ccp(CX-45,CY+5)];
		[self addChild:spBall1];
		spBall2 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall2 setScale:0.7f];
		[spBall2 setPosition: ccp(CX-45,CY-33)];
		[self addChild:spBall2];
		
		id rotate1 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat1 = [CCRepeatForever actionWithAction:rotate1];
		[spBall1 runAction:repeat1];	
		id rotate2 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat2 = [CCRepeatForever actionWithAction:rotate2];
		[spBall2 runAction:repeat2];
		
		//check image
		spCheck1 = [CCSprite spriteWithFile:@"option_check.png"];
		[spCheck1 setPosition: ccp(CX-50,CY+145)];
		[self addChild:spCheck1];
		spCheck2 = [CCSprite spriteWithFile:@"option_check.png"];
		[spCheck2 setPosition: ccp(CX-50,CY+111)];
		[self addChild:spCheck2];
		spCheck3 = [CCSprite spriteWithFile:@"option_check.png"];
		[spCheck3 setPosition: ccp(CX-50,CY+82)];
		[self addChild:spCheck3];
		spCheck4 = [CCSprite spriteWithFile:@"option_check.png"];
		[spCheck4 setPosition: ccp(CX-49,CY+52)];
		[self addChild:spCheck4];
		
		if ( gameInfo.blnFastball == FALSE )
			spCheck1.visible = FALSE;
		if ( gameInfo.blnCurveball == FALSE )
			spCheck2.visible = FALSE;
		if ( gameInfo.blnSlider == FALSE )
			spCheck3.visible = FALSE;
		if ( gameInfo.blnChangeup == FALSE )
			spCheck4.visible = FALSE;
		
		//etc
		isTouchEnabled = YES;
		
		[CCMenuItemFont setFontSize:25];
		[CCMenuItemFont setFontName:@"MarkerFelt-Thin"];
		
		CCMenuItem *option = [CCMenuItemFont itemFromString:@"start" target:self selector:@selector(startGame:)];
		[((CCMenuItemFont *)option).label setColor:ccc3(0,0,0)];	
		[option setPosition:ccp(0,5)];
		
		CCMenuItem *backToMenu = [CCMenuItemFont itemFromString:@"menu" target:self selector:@selector(backToMenu:)];
		[((CCMenuItemFont *)backToMenu).label setColor:ccc3(0,0,0)];	
		[backToMenu setPosition:ccp(0,-30)];
		
		CCMenu *menu = [CCMenu menuWithItems:option, backToMenu, nil];	
//		[menu alignItemsVertically];
		[self addChild:menu z:1];
	}
	return self;
}

-(void)startGame: (id)sender {
    BatteryView *gs = [BatteryView node];
    [[CCDirector sharedDirector] replaceScene: (CCScene*)gs];
	
	if ( !gameInfo.blnFastball && !gameInfo.blnCurveball &&	!gameInfo.blnSlider && !gameInfo.blnChangeup )
		gameInfo.blnFastball = TRUE;
}

-(void)backToMenu: (id)sender {
	Menu *ms = [Menu node];
    [[CCDirector sharedDirector] replaceScene: (CCScene*)ms];
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* touch = [touches anyObject];
	CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
	
	CGRect rectChect1 = CGRectMake(spCheck1.position.x-20, spCheck1.position.y-15, spCheck1.contentSize.width+20, spCheck1.contentSize.height+10);
	CGRect rectChect2 = CGRectMake(spCheck2.position.x-20, spCheck2.position.y-15, spCheck2.contentSize.width+20, spCheck2.contentSize.height+10);
	CGRect rectChect3 = CGRectMake(spCheck3.position.x-20, spCheck3.position.y-15, spCheck3.contentSize.width+20, spCheck3.contentSize.height+10);
	CGRect rectChect4 = CGRectMake(spCheck4.position.x-20, spCheck4.position.y-15, spCheck4.contentSize.width+20, spCheck4.contentSize.height+10);
	
	if ( CGRectContainsPoint(rectChect1, location) )
	{
		spCheck1.visible = !spCheck1.visible;
		gameInfo.blnFastball = !gameInfo.blnFastball;
	}
	else if ( CGRectContainsPoint(rectChect2, location) )
	{
		spCheck2.visible = !spCheck2.visible;
		gameInfo.blnCurveball = !gameInfo.blnCurveball;
	}
	else if ( CGRectContainsPoint(rectChect3, location) )
	{
		spCheck3.visible = !spCheck3.visible;
		gameInfo.blnSlider = !gameInfo.blnSlider;
	}
	else if ( CGRectContainsPoint(rectChect4, location) )
	{
		spCheck4.visible = !spCheck4.visible;
		gameInfo.blnChangeup = !gameInfo.blnChangeup;
	}
}

@end






























